const notice = {
  state: {
    // 未读消息总数
    noticeCount: 0,
    // socket服务器传来的消息
    socketMessage: {},
    messageTrigger: 0,
  },
  mutations: {
    SET_NOTICE_COUNT: (state, count) => {
      state.noticeCount = count;
    },
    SET_SOCKET_MESSAGE: (state, data) => {
      state.socketMessage = data;
      state.messageTrigger++;
    },
  },
  actions: {
    SetSocketMessage: ({ commit }, returnMsg) => {
      // console.log("returnMsg", returnMsg);
      const { noticeCount, flag, err_code } = returnMsg;
      if (
        (flag == "response" && err_code == 1000) ||
        noticeCount != undefined
      ) {
        commit("SET_NOTICE_COUNT", noticeCount || 0);
      }
      if (flag != "heartBeat" && flag != "init" && flag != "connect") {
        // 有新消息或者消息已读时触发监听器
        commit("SET_SOCKET_MESSAGE", returnMsg);
      }
    },
  },
};
export default notice;
